<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 5/8 0008
 * Time: 19:55
 */

namespace app\api\controller;


use app\api\model\Alipay;
use app\api\model\Appbuyorder;
use app\api\model\Applist;
use app\api\model\Userapp;
use app\api\model\Wxpay;
use think\Controller;
use think\Request;

class Market extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(!$request->isAjax()){
            die(json_encode(["status"=>0,"data"=>"","msg"=>"请求方式错误"]));
        }
    }

    /**
     * 获取应用市场列表
     * @return \think\response\Json
     */
    public function getapplist(){
        $model=new Applist();
        return json(["status"=>1,"data"=>$model->getlist()]);
    }

    /**
     * 获得一个应用的详情
     * @return \think\response\Json
     */
    public function getdetail(){
        $userid=Index::verifytoken(request()->post("token"));
        if(!$userid){
            return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
        }
        $shopid=model("manager")->where("id",$userid)->column("shopid")[0];
        if($shopid){
            return json(["status"=>0,"data"=>"","msg"=>"门店不存在"]);
        }
        $res=model("applist")->where("y_applist.id",request()->post("tableid"))->join("y_appdetail","y_appdetail.pid=y_applist.id","left")->field("y_appdetail.logourl,y_applist.*")->find();
        $useage=model("userapp")->where("shopid",$shopid)->where("appid",request()->post("tableid"))->find();
        return json(["status"=>1,"data"=>["detail"=>$res,"useage"=>$useage]]);
    }


    /**
     * 使用POS购买一个APP
     * @return \think\response\Json
     */
    public function buyapp(){
        $userapp=new Userapp();
        $userid=Index::verifytoken(request()->post("token"));
        if(!$userid){
            return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
        }
        $res=$userapp->posbuyapp($userid,\request()->post("appid"),\request()->post("year"),\request()->post("price"));
        if($res){
            return json(["status"=>1,"msg"=>"购买成功"]);
        }else{
            return json(["status"=>0,"msg"=>"购买失败"]);
        }
    }

    /**
     * 获取POS用户的应用列表
     * @return \think\response\Json
     */
    public function getuserlist(){
        $userapp=new Userapp();
        $userid=Index::verifytoken(request()->post("token"));
        if(!$userid){
            return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
        }
        $data=$userapp->getuserapp($userid);
        if($data===false){
            return json(["status"=>0,"data"=>"","msg"=>"用户没有门店"]);
        }else{
            return json(["status"=>1,"msg"=>"success","data"=>$data]);
        }
    }

    /**
     * 生成预订单并获得一个支付URL短网址
     * 2017年5月25日 10:52:41
     * @return \think\response\Json
     */
    public function preorder(){
        $appbuyorder=new Appbuyorder();
        $userid=Index::verifytoken(request()->post("token"));
        if(!$userid){
            return json(["status"=>-1,"data"=>"","msg"=>"用户鉴权失败"]);
        }
        $ordersn=$appbuyorder->makepre($userid,\request()->post("appid"),\request()->post("year"),\request()->post("price"));
        if($ordersn){
            if(request()->post("payway")==1){
                $wxpay=new Wxpay();
                $url=$wxpay->getqrurl($ordersn,\request()->post("price"));
            }else{
                $alipay=new Alipay();
                $url=$alipay->getqrurl($ordersn,\request()->post("price"));
            }
            //生成支付宝或微信短网址
            return json(["status"=>1,"msg"=>"生成预订单成功","data"=>["url"=>$url,"ordersn"=>$ordersn]]);
        }else{
            return json(["status"=>0,"msg"=>"生成预订单失败"]);
        }
    }

    /**
     * 查询支付状态
     */
    public function checkpayres(){
        $res=model("appbuyorder")->where("sn",\request()->post("ordersn"))->find();
        if($res and $res["	orderstatus"]==1){
            return json(["status"=>1]);
        }else{
            return json(["status"=>0]);
        }
    }

    /**
     * 查询充值列表
     */
    public function getchargelist(){
        return json(["status"=>0]);
    }

}